package com.example.surveyapplication;

import help.Utility;

import java.util.ArrayList;
import java.util.List;

import object.TestObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class TestActivity extends Activity implements OnClickListener {

	public static ListView listViewListTest;

	private List<TestObject> listTests;
	private TestListViewAdapter testAdapter;
	
	private TextView lblAnswer1;
	private TextView lblAnswer2;
	private TextView lblAnswer3;
	private TextView lblAnswer4;
	private TextView lblAnswer5;
	private TextView lblQuestion;
	private Button btnContinue;
	private int index;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	
		listViewListTest = (ListView) findViewById(R.id.lsvQuestion_Test);

		btnContinue = (Button) findViewById(R.id.btnContinue_Test);		
		btnContinue.setOnClickListener(this);
		
		lblAnswer1 = (TextView)findViewById(R.id.lblAnswer1_Test);
		lblAnswer2 = (TextView)findViewById(R.id.lblAnswer2_Test);
		lblAnswer3 = (TextView)findViewById(R.id.lblAnswer3_Test);
		lblAnswer4 = (TextView)findViewById(R.id.lblAnswer4_Test);
		lblAnswer5 = (TextView)findViewById(R.id.lblAnswer5_Test);
		lblQuestion = (TextView)findViewById(R.id.lblQuestion_Test);	
		index = 0;
		loadContent(index);
	}
	
	private void loadContent(int titleIndex)
	{
		lblQuestion.setText(Utility.lstTitleDTO.get(titleIndex).getContent());

		listTests = new ArrayList<TestObject>();
		for (int i = 0; i < Utility.lstTitleDTO.get(titleIndex).lstQuestion.size(); i++) {
			ArrayList<Boolean> ans = new ArrayList<Boolean>();
			listTests.add(new TestObject(titleIndex, i));
		}

		testAdapter = new TestListViewAdapter(this, listTests);
		listViewListTest.setAdapter(testAdapter);
		
		if (Utility.lstTitleDTO.get(titleIndex).lstAnswer.size() == 5) {
			lblAnswer1.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(0).getContent());
			lblAnswer2.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(1).getContent());
			lblAnswer3.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(2).getContent());
			lblAnswer4.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(3).getContent());
			lblAnswer5.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(4).getContent());
		}
		else {
			lblAnswer1.setText("");
			lblAnswer2.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(0).getContent());
			lblAnswer3.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(1).getContent());
			lblAnswer4.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(2).getContent());
			lblAnswer5.setText(Utility.lstTitleDTO.get(titleIndex).lstAnswer.get(3).getContent());
		}
		index++;
	}
	
	private void showAnswer(int titleIndex) {
		String mess = "Answer: ";
		for (int i = 0; i < listTests.size(); i++) {
			mess += String.valueOf(listTests.get(i).getAnswerIndex());
		}
		mess += "\nNo answer: ";
		mess += String.valueOf(getNoAnswered());
		mess += "\nScore: ";
		mess += String.valueOf(getScore());
		showAlert(mess);
	}
	private int getNoAnswered() {
		int count = 0;
		for (TestObject testObj : listTests) {
			if (testObj.getAnswerIndex() != -1) {
				count ++;
			}
		}
		return count;
	}
	private int getScore() {
		int score = 0;
		for (TestObject testObj : listTests) {
			score += testObj.getScore();
		}
		return score;
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.btnContinue_Test:
			int noAnswered = getNoAnswered();
			if (noAnswered < listTests.size()) {
				showAlert("Please complete this test.");
			}
			else {
				Utility.createTestDetail(listTests, Utility.lstTitleDTO.get(index-1).getID());
				if (index < Utility.lstTitleDTO.size() - 1) {
					loadContent(index);
				}
				else if (index == Utility.lstTitleDTO.size() - 1) {
					loadContent(index);
					btnContinue.setText("Finish");
				}
				else {
					Intent intent = new Intent(this, ThankActivity.class);
					startActivity(intent);
				}
			}
			break;
		default:
			break;
		}
		
	}

	private void showAlert(String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("");
		alertDialog.setMessage(message);
		alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				}
				});
		alertDialog.show();
	}
}
